/**
 * 
 */
package com.pactera.integral.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.pactera.integral.bean.UserBean;
import com.pactera.integral.common.repository.QueryResult;
import com.pactera.integral.common.repository.ResultMap;
import com.pactera.integral.dao.UserDao;
import com.pactera.integral.dao.UserGroupDao;
import com.pactera.integral.entity.User;
import com.pactera.integral.entity.UserGroup;
import com.pactera.integral.service.UserService;

/**
 * @author TempUser
 *
 */
@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;


	@Autowired
	private UserGroupDao userGroupDao;


	/**
	 * 添加或修改用户
	 * @param user
	 * @return Boolean
	 */
	@Override
	public Boolean saveUser(User user) {
		boolean flag = false; 
		User u = userDao.findByUserId(user.getUserId());
		if(null != u){
			userGroupDao.deleteUserGroup(user.getUserId());
			//校验传入的StaffId和本条记录的StaffId是否相同
			if(!u.getStaffId().equals(user.getStaffId())){
				//校验是否已有StaffId
				if(null != userDao.findByStaffId(user.getStaffId())){
					
					return Boolean.FALSE;
				}	
			}
			flag = true;
		}
		if(!flag){
			if(null != userDao.findByStaffId(user.getStaffId())){
				
				return Boolean.FALSE;
			}
		}
		
		user.setCreatedTime(new Date());
		user.setEnabled(true);
		user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
		userDao.save(user);
		if(null != user.getGroupIds()){
			//用户关联group
			for (String groupId : user.getGroupIds()) {
				UserGroup group=new UserGroup();
				group.setGroupId(groupId);
				group.setUserId(user.getUserId());
				userGroupDao.save(group);	
			}	
			return Boolean.TRUE;
		}
		
		return Boolean.FALSE;
	}
	
	/**
	 * 删除用户
	 * @param userId
	 * @return Boolean
	 */
	@Override
	public Boolean deleteUser(String userId) {
		User user = userDao.findByUserId(userId);
		user.setEnabled(false);
		//userDao.saveAndFlush(user);
		userDao.delete(user);
		return Boolean.TRUE;
	}
	
	/**
	 * 用户明细
	 * @param userId
	 * @return UserBean
	 */
	@Override
	public UserBean getUser(String userId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userId", userId);
		QueryResult<ResultMap<String, Object>> queryResult = userDao.findMapListBySQLKey("sysuser.userdetails", paramMap,null);
		List<UserGroup> userGroupList = userGroupDao.findByUserId(userId);
		UserBean bean = new UserBean();
		if(queryResult != null){
			ResultMap<String, Object> map = queryResult.getContent().get(0);
			bean.setUserId(map.get("userId").toString());
			bean.setCreatedBy(map.get("createdBy").toString());
			bean.setCreatedTime(new Date());
			bean.setLastUpdatedBy(map.get("lastUpdatedBy").toString());
			bean.setLastUpdatedTime(new Date());
			bean.setDepartmentCode(map.get("departmentCode").toString());
			bean.setDepartmentName(map.get("departmentName").toString());
			bean.setName(map.get("userName").toString());
			bean.setPassword(map.get("password").toString());
			bean.setPhone(map.get("phone").toString());
			bean.setStaffId(map.get("staffId").toString());
			String groupIds = "";
			for (UserGroup userGroup : userGroupList) {
				groupIds = groupIds+userGroup.getGroupId()+",";
			}
			bean.setUserGroupIds(groupIds.split(","));
		}
		
		return bean;
	}
	
	/**
	 * 用户列表
	 * @param parameterMap
	 * @param page
	 * @return QueryResult<ResultMap<String, Object>>
	 */
	@Override
	public QueryResult<ResultMap<String, Object>> searchUserList(
			Map<String, Object> paramMap, Pageable page) {
			
		return userDao.findMapListBySQLKey("sysuser.search.list", paramMap, page);
	}
	
	

	
}
